package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.PermissionException;
import com.itheima.exception.UserException;
import com.itheima.pojo.Permission;
import com.itheima.pojo.User;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findPage(queryPageBean);
    }


    @RequestMapping("add")
    public Result add(@RequestBody Permission permission){

        boolean res = permissionService.add(permission);

        if(res){
            return Result.success("");
        }else {
            return Result.error("");
        }
    }


    @RequestMapping("deletebyid")
    public Result deleteById(Integer id){

        try {
            permissionService.deleteById(id);
            return Result.success("");
        } catch (PermissionException e) {
            e.printStackTrace();
            return Result.error("");
        }
    }


    @RequestMapping("findbyid")
    public Result findbyid(String id){

        try {
            Permission permission = permissionService.findById(id);
            return Result.success("",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }

    }


    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permission){

        boolean res = permissionService.edit(permission);

        if(res){
            return Result.success("");
        }else {
            return Result.error("");
        }
    }

    @RequestMapping("findall")
    public Result findall(){

        try {
            List<Permission> permissions= permissionService.findall();
            return Result.success("",permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }
}
